package com.xuhai.blog.admin.controller;


import com.xuhai.blog.admin.entity.ArticleEntity;
import com.xuhai.blog.admin.service.ArticleService;
import com.xuhai.blog.admin.service.CategoryService;
import com.xuhai.blog.common.util.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author 机蜂
 * @since 2022-02-08
 */
@RestController
@RequestMapping("/admin/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public ResultJson page(int page, int limit, ArticleEntity article){
        return ResultJson.ok().data(articleService.getPage(page, limit, article));
    }

    @PostMapping("/update")
    public ResultJson update(@RequestBody ArticleEntity article){
        articleService.update(article);
        return ResultJson.ok();
    }

    @PostMapping("/status")
    public ResultJson status(@RequestBody ArticleEntity article){
        articleService.updateById(article);
        return ResultJson.ok();
    }

    @DeleteMapping("/delete/{articleId}")
    public ResultJson delete(@PathVariable Integer articleId){
        articleService.delete(articleId);
        return ResultJson.ok();
    }

    @GetMapping("/category")
    public ResultJson category(){
        return ResultJson.ok().data(categoryService.select());
    }

    @GetMapping("/info/{articleId}")
    public ResultJson info(@PathVariable Integer articleId){
        return ResultJson.ok().data(articleService.getById(articleId));
    }
}

